<?php


namespace App\Console\Commands;


use App\Models\DataDict;
use App\Models\District;
use Illuminate\Console\Command;

class InitStarredFamilyHotel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:init-starred-family-hotel';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    public function handle()
    {
        $fileName = '星级饭店extra.xlsx';
        $data = $this->excelToArray($fileName);
        foreach ($data as $v) {
            if (!empty($v['星级饭店（酒店）名称'])) {
                $dictCode = $v['星级饭店（酒店）名称'];
                $model = DataDict::where(['category_code' => 'StarredHotel', 'dict_code' => $dictCode])->first();
                $extra = ['星级' => $v['星级'], '客房数' => (string)$v['客房数'], '床位数' => (string)$v['床位数']];
                $model->extra = $extra;
                $model->save();
            }
        }
        print_r($data);
        exit;
    }

    public function handle2()
    {
        $arr = ['达热瓦大酒店', '喜嘎央恰大酒店', '山东大厦酒店', '雍泽大酒店', '唐卡艺术酒店',
            '德青林大酒店', '浩月宫大酒店', '刚坚宾馆', '旺润国际大酒店', '乔穆朗宗大酒店', '朝日宫廷大酒店',
            '格萨尔大酒店', '哈达神湖酒店', '龙珠颇章饭店', '坤逸星光酒店', '江孜饭店',
            '江沅氧吧酒店', '玉妥宾馆', '明湖宾馆', '雪山圣渔大酒店', '亚星大厦酒店', '亚东平措康桑康养酒店',
            '老村长酒店', '定日珠峰宾馆', '世界屋脊大酒店', '白坝大酒店', '岗嘎宾馆',
            '哈呼宾馆', '雪豹客栈', '潘多宾馆', '元府大酒店', '欧曲雅培大酒店', '萨嘎宾馆',
            '西部驿站', '雅江源宾馆'];

        $result = [];
        foreach ($arr as $v) {
            $model = DataDict::where(['dict_code' => $v])->first();
            if (empty($model)) {
                $result[] = $v;
            }
        }
        print_r($result);
        exit;

        echo count($arr);
        exit;

        $fileName = '星级家庭旅馆.xlsx';
        $data = $this->excelToArray($fileName);

        foreach ($data as $v) {
            $extra['星级'] = trim($v['星级']);
            $model = new DataDict();
            $model->dict_code = trim($v['旅馆名称']);
            $model->dict_desc = trim($v['旅馆名称']);
            $model->category_code = 'StarredFamilyHotel';
            $model->category_desc = '星级家庭旅馆';
            $model->district_id = $this->getRowDistrictId($v);
            $model->extra = $extra;
            $model->type = DataDict::TYPE_DISTRICT;
            $model->save();
        }
    }

    private function excelToArray($fileName)
    {
        $filePath = __DIR__ . '/../../../storage/app/public/dict/' . $fileName;
        $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
        $sheet = $objPHPExcelReader->getSheet(0);
        $data = $sheet->toArray();
        $keyArr = $data[0];
        $result = [];

        foreach ($data as $k1 => $v1) {
            if ($k1 == 0) {
                continue;
            }
            $tmpData = [];
            foreach ($v1 as $k2 => $v2) {
                $tmpData[$keyArr[$k2]] = $v2;
            }
            $result[] = $tmpData;
        }

        return $result;
    }


    private function getRowDistrictId($row)
    {
        if (!isset($row['县（区）']) || empty(trim($row['县（区）']))) {
            return null;
        }
        $districtName = trim($row['县（区）']);
        if (empty($districtName)) {
            return null;
        }
        $model = District::where('name', 'like', "%{$districtName}%")->first();
        return !empty($model) ? $model->id : null;
    }
}
